
{% extends "base.html" %} 
{% block head %}
<script src="/js/index.js" type="text/javascript"></script>
{% endblock %}
  
{% block content %}
    <h1>Your CCR Profile Info Below</h1>

    <div id="medications"><h2>Medications</h2></div>
    <div id="conditions"><h2>Conditions</h2></div>
 

    <hr>
        
    <div class="debug_bar">
    <!--  
       <b>DEBUG:</b> {{ profile_feed }}<br>
       <p> medications:{{ medications }}</p>
       <p> conditions:{{ conditions }}</p>
       <p> immunizations:{{ immunizations }}</p>
       <p> procedures:{{ procedures }}</p>
       <p> test_results:{{ test_results }}</p>
       <p> demographics:{{ demographics }}</p>
     -->
    </dir> 

    <script type="text/javascript" charset="utf-8">
    function reverseCompareToDate(a, b) {
    	dateA = Date.parse(a.to);
    	dateB = Date.parse(b.to);
    	if (dateA < dateB) {return 1;} 
    	if (dateA > dateB) {return -1;}
    	if (dateA == dateB) {return 0;}
    }              
    function reverseCompareStartDate(a, b) {
    	dateA = Date.parse(a.start_date);
    	dateB = Date.parse(b.start_date);
    	if (dateA < dateB) {return 1;} 
    	if (dateA > dateB) {return -1;}
    	if (dateA == dateB) {return 0;}
    }
//{"from": "2009-08-03", "name": "Amoxicillin", "route": "By mouth", "to": "2009-08-31", "frequency": "every 4 hours", "pill_strength": "125 mg"}
		var currentMeds = $('<div>');
		currentMeds.append('<table >');
		var pastMeds = $('<div>');
		pastMeds.append('<table>');
    	var m = {{ medications }}.sort(reverseCompareToDate);
		for(var i in m) {
			var medication = m[i];
			var name = medication.name;
			var cell_1 = medication.name+ ' taken ' + medication.route + ' ' + medication.frequency;
			var cell_2 = medication.from + ' to ' + medication.to;	
			
			if (Date.today().getTime() <= Date.parse(medication.to)) {
				// present medications
				currentMeds.append('<tr><td class="desc">'+cell_1+'</td><td class="date">'+cell_2+'</td></tr>');
			} else {
				// past medications
				pastMeds.append('<tr><td class="desc">'+cell_1+'</td><td class="date">'+cell_2+'</td></tr>');
			}
		}
		currentMeds.append('</table>');
		currentMeds.append('</div>');
		pastMeds.append('</table>');
		pastMeds.append('</div>');
		
		$('#medications').append('<h3>CURRENT MEDS</h3>');
		$('#medications').append(currentMeds);
		$('#medications').append('<h3>PAST MEDS</h3>');
		$('#medications').append(pastMeds);

		//{"status": "Active", "actor_role": null, "code": "9.102", "treated_by": "janetcbrownstone@gmail.com", "name": "Abdominal Cramps"}
		var currentConditions = $('<div>');
		var pastConditions = $('<div>');
		currentConditions.append('<h3>Current Conditions</h3>');
		pastConditions.append('<h3>Past Conditions</h3>');
     var c = {{ conditions }}.sort(reverseCompareStartDate);
		for(var i in c) {
			var condition = c[i];
			cell_1 = condition.name+'  <span class="small">IDC9:'+condition.code+'</span> '; 
			cell_2 = '';
			if (condition.start_date) {cell_2 += '  '+condition.start_date; }
			if (condition.end_date) { cell_2 += ' to '+condition.end_date; }  
			
			if (condition.status.toLowerCase() == 'active') {
				currentConditions.append('<tr><td class="desc">'+cell_1+'</td><td class="date">'+cell_2+'</td></tr>');
			} else {
				pastConditions.append('<tr><td class="desc">'+cell_1+'</td><td class="date">'+cell_2+'</td></tr>');
			}
		}
		currentConditions.append('</div>');
		pastConditions.append('</div>');
		$('#conditions').append(currentConditions);
		$('#conditions').append(pastConditions);
		
		
    </script>

{% endblock %} 


     
  